#ifndef UDP_RECEIVE_H
#define UDP_RECEIVE_H

#include <QMainWindow>
#include "ui_mainwindow.h"
#include <QObject>
#include <QUdpSocket>

class UdpReceiver : public QObject {
    Q_OBJECT

public:
    explicit UdpReceiver(Ui::MainWindow *_ui, QObject *parent = nullptr);
    ~UdpReceiver();

    // 启动接收器，监听指定端口
    void startReceiver(quint16 port);

    // 停止接收器
    void stopReceiver();

    // 处理接收到的字节流
    void dataProcess(QByteArray& data);

private slots:
    // 读取接收到的数据
    void readPendingDatagrams();

private:
    Ui::MainWindow *ui;
    QUdpSocket *udpSocket;
    uint64_t cur_pic;
    QByteArray img_array;
    bool first_in;
    int current_pack;                                           // 当前图片收到第几个包
    const int PER_PIC_SIZE = 1024;                              // 每次收到的包中图片数据字节数
    struct PackageHeader
    {
        char Header = 0x32;                                     // Header
        char last = 0;                                          // state
        uint16_t last_num = 0;
        int cur_pack = 0;
        uint64_t cur_pic = 0;
    };
    PackageHeader P_head;
    const int PACK_SIZE = PER_PIC_SIZE+sizeof(PackageHeader);   //每个包大小 1040
};

#endif // UDP_RECEIVE_H
